package controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import dao.UserDao;
import dao.StudiengangDao;

public class AdminRegisterSave extends AbstractController {
	
	private UserDao userDao;
	private StudiengangDao studiengangDao;
	
	public StudiengangDao getStudiengangDao() {
		return studiengangDao;
	}

	public void setStudiengangDao(StudiengangDao studiengangDao) {
		this.studiengangDao = studiengangDao;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	protected ModelAndView handleRequestInternal(
			HttpServletRequest httpServletRequest,
			HttpServletResponse httpServletResponse) throws Exception {
		httpServletRequest.setCharacterEncoding("UTF-8");
		String username = httpServletRequest.getParameter("username");
		int iemail = Integer.parseInt(httpServletRequest.getParameter("email"));
		if(iemail == 1)
		{
			username = username.concat("@stud.fh-deggendorf.de");
		}
		else if(iemail == 2)
		{
			username = username.concat("@fh-deggendorf.de");
		}
		
		String password = httpServletRequest.getParameter("password2");
		//Studiengang studiengang = Integer.parseInt(httpServletRequest.getParameter("cStudiengang"));
		//erzeuge neuen User in DB
		//getUserDao().create(username, password , "admin", studiengang);
		getUserDao().create(username, password , "admin", studiengangDao.findById(Integer.parseInt(httpServletRequest.getParameter("cStudiengang"))));
				ModelAndView mv = new ModelAndView();
				mv.addObject("new_admin", username);
				mv.setViewName("admin_register_success");
				return mv;
	}

}
